$package('Bdth.Device');
Bdth.Device = function () {
	var _box = null;
	var _this = {
		save: function () {
			_box.handler.save();
		},
		config: {
			event: {
				add: function () {
					//	_box.handler.add();
					$("#edit-win").dialog({
						href: path + '/view/device/form.html',
						width: 600,
						height: 400,
						modal: true,
						resizable: true,
						/*	collapsible: true,
						 maximizable: true,*/
						onLoad: function () {
							$("#editForm").resetForm();
							$('#speedLimit').numberbox('setValue', 80);
							$("#lastCheckTime").iDatetimebox('setValue', new Date().Format("yyyy-MM-dd hh:mm:ss"));
						},
						title: '添加',
						closable: true,
						buttons: [{
							text: '保存',
							iconCls: 'fa fa-save',
							btnCls: 'topjui-btn',
							handler: function () {
								if ($("#editForm").form('validate')) {
									Bdth.progress();
									$("#editForm").attr('action', 'save');
									var startTime = $("#startTime").val();
									var endTime = $("#endTime").val();
									if (startTime >= endTime) {
										Bdth.closeProgress();
										$.iMessager.alert('提示', '开始服务时间不能大于等于结束服务时间', 'messager-warning');
										return false;
									}
									var lastCheckTime = $("#lastCheckTime").val();
									var data = {
										"startTimeHidden": startTime,
										"endTimeHidden": endTime,
										"lastCheckTimeHidden": lastCheckTime
									};//附加的数据
									Bdth.saveForm($("#editForm"), data, function (data) {
										Bdth.closeProgress();
										$("#edit-win").dialog('close');
										var param = $("#searchForm").serializeObject();
										$('#data-list').datagrid('reload', param);
										$("#editForm").resetForm();
									});
								}
							}
						}],
					});

				},
				edit: function () {
					var record = Utils.getCheckedRows();
					if (Utils.checkSelectOne(record)) {
						Bdth.progress();
						var data = {};
						var idKey = 'id'; // 主键名称
						data[idKey] = (record[0][idKey]);
						Bdth.getById('getId', data, function (result) {
							Bdth.closeProgress();
							$("#edit-win").dialog({
								href: path + '/view/device/form.html',
								width: 600,
								height: 400,
								modal: true,
								resizable: true,
								/*	collapsible: true,
								 maximizable: true,*/
								onLoad: function () {
									$("#editForm").form('load', result.data);
									$("#communicationModel option[value='" + result.data.communicationModel + "']").attr("selected", true);
									var value = result.data.communicationModel;
									if (value == 'GPRS') {
										$("#simNoFont").html('设备号');
										$("#simNoTd1").show();
										$("#simNoTd2").show();
										$("#bdsimNo1").hide();
										$("#bdsimNo2").hide();
										$('#simIOT').validatebox({required: false});
									}
									if (value == 'RDSS') {
										$("#simNoFont").html('设备号');
										$("#simNoTd1").show();
										$("#simNoTd2").show();
										$("#bdsimNo1").hide();
										$("#bdsimNo2").hide();
										$('#simIOT').validatebox({required: false});
									}
									if (value == 'RDSS+GPRS') {
										$("#simNoFont").html('设备号');
										$('#simIOT').validatebox({required: false});
										$("#simNoTd1").show();
										$("#simNoTd2").show();
										$("#bdsimNo1").show();
										$("#bdsimNo2").show();
										/*	if ($.trim($("#simIOT").val()) != '') {
										 $("#simIOT").removeClass('depInputClass');
										 }*/
									}

									$("#typeID option[value='" + result.data.typeID + "']").attr("selected", true);

									$("#edit-department").removeClass('depInputClass');
									//$("#simIOT").removeClass('depInputClass');
								},
								title: '编辑',
								buttons: [{
									text: '保存',
									iconCls: 'fa fa-save',
									btnCls: 'topjui-btn',
									handler: function () {
										if ($("#editForm").form('validate')) {
											Bdth.progress();
											$("#editForm").attr('action', 'save');
											var startTime = $("#startTime").val();
											var endTime = $("#endTime").val();
											if (startTime >= endTime) {
												Bdth.closeProgress();
												$.iMessager.alert('提示', '开始服务时间不能大于等于结束服务时间', 'messager-warning');
												return false;
											}
											var lastCheckTime = $("#lastCheckTime").val();
											var data = {
												"startTimeHidden": startTime,
												"endTimeHidden": endTime,
												"lastCheckTimeHidden": lastCheckTime
											};//附加的数据
											Bdth.saveForm($("#editForm"), data, function (data) {
												Bdth.closeProgress();
												$("#edit-win").dialog('close');
												var param = $("#searchForm").serializeObject();
												$('#data-list').datagrid('reload', param);
												$("#editForm").resetForm();
											});
										}
									}
								}/*, {
								 text: "关闭",
								 iconCls: "fa fa-close",
								 btnCls: "topjui-btn-danger",
								 handler: _box.handler.close
								 }*/],
							});

						});
					}
				},
				save: function () {
					_this.save();
				}
			},
			dataGrid: {
				title: '设备列表',
				url: 'dataList',
				//queryParams: { "username": username },
				columns: [[
					{field: 'id', checkbox: true, title: 'id'},
					{field: 'equipName', title: '显示名称', width: 120, sortable: false},
					{field: 'simNo', title: '设备号', width: 120, sortable: false},
					{field: 'communicationModel', title: '通信模式', width: 60, sortable: false},
					{field: 'depName', title: '所属组', width: 120, sortable: false},
					{field: 'startTime', title: '开始服务时间', width: 120, sortable: false},
					{field: 'endTime', title: '结束服务时间', width: 120, sortable: false},
				]],
				toolbar: [
					{id: 'btnadd', text: '添加', btnType: 'add'},
					{id: 'btnedit', text: '修改', btnType: 'edit'},
					{id: 'btndelete', text: '删除', btnType: 'remove'}
				]
			}, action: {
				add_editForm: '/view/device/form.html',
				remove: 'fakeDelete'
			},
			wid_height: {
				width: 600,
				height: 330
			},
		},
		init: function () {
			_box = new DataGrid(_this.config);
			_box.init();
		}
	}
	return _this;
}();

$(function () {
	Bdth.Device.init();
});
// Grid 工具类
var Utils = {
	getCheckedRows: function () {
		return $('#data-list').datagrid('getChecked');
	},
	checkSelect: function (rows) {// 检查grid是否有勾选的行, 有返回 true,没有返回true
		var records = rows;
		if (records && records.length > 0) {
			return true;
		}
		Bdth.alert('警告', '未选中记录.', 'messager-warning');
		return false;

	},
	checkSelectOne: function (rows) {// 检查grid是否只勾选了一行,是返回 true,否返回true
		var records = rows;
		if (!Utils.checkSelect(records)) {
			return false;
		}
		if (records.length == 1) {
			return true;
		}
		Bdth.alert('警告', '只能选择一行记录.', 'messager-error');
		return false;
	}
}